package com.renwen.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.renwen.sms.dao.TeacherDao;
import com.renwen.sms.dto.*;
import com.renwen.sms.entity.Teacher;

import com.renwen.sms.exception.MyRuntimeException;
import com.renwen.sms.service.TeacherService;
import com.renwen.sms.util.Result;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * teacher控制器
 *
 * @author zuo, shunnian
 */
@RestController
@CrossOrigin
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;
    @Resource
    private TeacherDao teacherDao;

    /**
     * 获取所有的教师列表
     *
     * @param page 页码
     * @param size 每页显示条数
     * @return 教师列表  select * from xx limit 5, 5;
     */
    @PostMapping("/list")
    public PageResult<TeacherDto> list(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return teacherService.list(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException("目前还没有数据。");
        }
    }

    /**
     * 根据教师id编号查询信息
     *
     * @param id 编号
     * @return 教师信息
     */
    @GetMapping("/getById")
    public Result get(Integer id) {
        try {
            return teacherService.get(id);
        } catch (Exception e) {
            throw new MyRuntimeException("系统错误，嘻嘻！");
        }
    }

    /**
     * 添加教师
     *
     * @param teacher 教师对象
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Teacher teacher) {
        try {
            teacherService.save(teacher);
            return new Result("添加成功！", teacher.getTNo());
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 修改教师信息
     *
     * @param teacher 教师对象
     * @return 操作提示信息
     */
    @PostMapping("/update")
    public Result update(@RequestBody Teacher teacher) {
        try {
            teacherService.update(teacher);
            return new Result("修改成功！", teacher.getTNo());
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 删除教师
     *
     * @param tNo 教师号
     * @return 操作提示信息
     */
    @GetMapping("/delete")
    public Result del(String tNo) {
        try {
            teacherService.delete(tNo);
            return new Result("删除成功！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 根据教师编号组批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping("/batchDel")
    public Result batchDel(String ids) {
        try {
            teacherService.batchDelete(ids);
            return new Result("删除成功！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 导出教师表信息到桌面
     */
    @GetMapping("/out")
    public Result outStudent() {
        try {
            QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("t_no", "t_name", "t_sex", "t_age", "entry_year");
            // 根据编号降序排序
            queryWrapper.orderByDesc("t_no");
            List<Teacher> teachers = teacherDao.selectList(queryWrapper);
            //创建工作簿 类似于创建Excel文件
            XSSFWorkbook workbook = new XSSFWorkbook();
            //创建 sheetname页名
            XSSFSheet sheet = workbook.createSheet("教师信息");
            sheet.setColumnWidth(3, 20 * 256);//给第3列设置为20个字的宽度
            sheet.setColumnWidth(4, 20 * 256);//给第4列设置为20个字的宽度
            //创建一行,下标从0开始
            XSSFRow row = sheet.createRow(0);
            //创建这行中的列,下标从0开始 （表头）
            XSSFCell cell = row.createCell(0);
            // 给cell 0下表赋值
            cell.setCellValue("教师号");
            //创建这行中的列,并给该列直接赋值
            row.createCell(1).setCellValue("姓名");
            row.createCell(2).setCellValue("性别");
            row.createCell(3).setCellValue("年龄");
            row.createCell(4).setCellValue("入职时间");
            int i = 0;
            for (Teacher teacher : teachers) {
                // 设置表里内容
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(teacher.getTNo());
                row.createCell(1).setCellValue(teacher.getTName());
                row.createCell(2).setCellValue(teacher.getTSex());
                row.createCell(3).setCellValue(teacher.getTAge());
                row.createCell(4).setCellValue(teacher.getEntryYear());
                i++;
            }
            File file = new File("C:\\Users\\zuo\\Desktop\\教师信息表.xlsx");
            FileOutputStream stream = new FileOutputStream(file);
            // 需要抛异常
            workbook.write(stream);
            //关流
            stream.close();
            return new Result("导出成功！");
        } catch (Exception e) {
            return new Result(500, e.getMessage());
        }
    }

    /**
     * 查询所教学生信息
     *
     * @param page    页
     * @param size    尺寸
     * @param formMap 表格信息
     * @return xuanKeDto集合
     */
    @PostMapping("studentList")
    public PageResult<XuanKeDto> studentTeaList(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return teacherService.studentList(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException("目前还没有数据。");
        }
    }

    /**
     * 获取未安排课程的教师列表
     */
    @PostMapping("/noCnoList")
    public Result noCnoList() {
        try {
            return new Result("获取成功！", teacherService.noCnoList());
        } catch (Exception e) {
            throw new MyRuntimeException(e.getMessage());
        }
    }

    /**
     * 获取未安排班级的教师列表
     */
    @PostMapping("/noClsNoList")
    public Result noClsNoList() {
        try {
            return new Result("获取成功！", teacherService.noClsNoList());
        } catch (Exception e) {
            throw new MyRuntimeException(e.getMessage());
        }
    }

    /**
     * 根据教师号查询当前教师的课程信息
     *
     * @param page    页
     * @param size    尺寸
     * @param formMap 表格信息
     * @return
     */
    @PostMapping("course")
    public PageResult<CourseDto> myCourse(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return teacherService.myCourse(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException("目前还没有数据。");
        }
    }

    /**
     * 根据教师号查询当前教师的班级信息
     *
     * @param page    页
     * @param size    尺寸
     * @param formMap 表格信息
     * @return
     */
    @PostMapping("class")
    public PageResult<ClassDto> myClass(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return teacherService.myClass(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException("目前还没有数据。");
        }
    }

    /**
     * 根据教师号查询当前教师的班级学生信息
     *
     * @param page    页
     * @param size    尺寸
     * @param formMap 表格信息
     * @return
     */
    @PostMapping("student")
    public PageResult<StudentDto> myStudent(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return teacherService.myStudent(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException("目前还没有数据。");
        }
    }

    /**
     * 教师添加学生成绩
     *
     * @param grade 成绩信息
     * @return 添加结果
     */
    @PostMapping("addGrade")
    public Result addGrade(@RequestBody Grade grade) {
        try {
            int i = teacherService.addGrade(grade);
            if (i == 1) {
                return new Result(200, "操作成功");
            } else {
                return new Result(400, "操作失败");
            }
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 根据选课id查询学生成绩
     */
    @GetMapping("getGradeById")
    Result getGradeById(String id) {
        try {
            Grade grade = teacherService.getGradeById(id);
            if (grade != null) {
                grade.setId(id);
                return new Result("ok", grade);
            }
            return new Result(400, "查询失败！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }
}
